﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.yy.ui.controls {
    import flash.events.*;
    import flash.utils.*;
    import flash.display.*;
    import com.fminutes.vinciUI.events.*;
    import com.fminutes.vinciUI.core.*;
    import flash.text.*;
    import com.fminutes.vinciLib.utils.*;

    public class NumberSlider extends UIComponent {

        public static const LAYOUT_HORIZONTAL:String = "horizontal";
        public static const LAYOUT_VERTICAL:String = "vertical";
        public static const TXT_LAYOUT_CUT:String = "cut";
        public static const TXT_LAYOUT_MERGE:String = "merge";

        private var nextBtn:DisplayObject;
        private var prevBtn:DisplayObject;
        private var maxBtn:DisplayObject;
        private var minBtn:DisplayObject;
        private var numTxt:TextField;
        private var layout:String;
        private var txtLayout:String;
        private var distance:int;
        private var btnDistance:int = 2;
        private var txtBackground:DisplayObject;
        private var numMax:int;
        private var numMin:int;
        private var currentNum:int;
        public var stepNum:int;
        private var time:int;
        private var oldCurrentNum:int = 0;
        private var _h:Number = 0;
        private var _w:Number = 0;

        public function NumberSlider(_arg1:TextField, _arg2:DisplayObject=null, _arg3:int=0, _arg4:int=5, _arg5:String="merge", _arg6:DisplayObject=null, _arg7:DisplayObject=null, _arg8:DisplayObject=null, _arg9:DisplayObject=null, _arg10:String="horizontal"){
            this.txtBackground = _arg2;
            this.numTxt = _arg1;
            this.numMax = _arg3;
            this.distance = _arg4;
            this.txtLayout = _arg5;
            this.nextBtn = _arg6;
            this.prevBtn = _arg7;
            this.maxBtn = _arg8;
            this.minBtn = _arg9;
            this.layout = _arg10;
            super();
            this.init();
        }
        private function init():void{
            this.stepNum = 1;
            this.numTxt.restrict = "0-9";
            this.numMin = 0;
            this.currentNum = this.numMin;
            if (this.txtBackground){
                addChild(this.txtBackground);
            };
            this.changeNumTxt();
            addChild(this.numTxt);
            this.numTxt.addEventListener(Event.CHANGE, this.txtOnChange);
            this.numTxt.addEventListener(MouseEvent.CLICK, this.numTxtOnClick);
            if (this.nextBtn){
                addChild(this.nextBtn);
                this.nextBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
                this.nextBtn.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
            };
            if (this.prevBtn){
                addChild(this.prevBtn);
                this.prevBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
                this.prevBtn.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
            };
            if (this.maxBtn){
                addChild(this.maxBtn);
                this.maxBtn.addEventListener(MouseEvent.CLICK, this.maxBtnOnClick);
            };
            if (this.minBtn){
                addChild(this.minBtn);
                this.minBtn.addEventListener(MouseEvent.CLICK, this.minBtnOnClick);
            };
        }
        private function numTxtOnClick(_arg1:MouseEvent):void{
            this.numTxt.setSelection(0, this.numTxt.length);
        }
        private function txtOnChange(_arg1:Event):void{
            this.currentNum = int(this.numTxt.text);
            if (this.currentNum < 1){
                this.currentNum = 0;
            };
            if (this.currentNum > this.numMax){
                this.currentNum = this.numMax;
            };
            this.changeNumTxt();
        }
        private function onMouseDown(_arg1:MouseEvent):void{
            this.time = getTimer();
            if (_arg1.currentTarget == this.nextBtn){
                this.nextBtn.addEventListener(Event.ENTER_FRAME, this.onEnterNext);
                this.nextBtn.addEventListener(MouseEvent.MOUSE_OUT, this.onMouseUp);
            } else {
                this.prevBtn.addEventListener(Event.ENTER_FRAME, this.onEnterPrev);
                this.prevBtn.addEventListener(MouseEvent.MOUSE_OUT, this.onMouseUp);
            };
        }
        private function onEnterNext(_arg1:Event):void{
            var _local2:int = (getTimer() - this.time);
            if (_local2 > 400){
                this.nextNum();
            };
        }
        private function onEnterPrev(_arg1:Event):void{
            var _local2:int = (getTimer() - this.time);
            if (_local2 > 400){
                this.prevNum();
            };
        }
        private function onMouseUp(_arg1:MouseEvent):void{
            if (this.nextBtn.hasEventListener(Event.ENTER_FRAME)){
                this.nextBtn.removeEventListener(Event.ENTER_FRAME, this.onEnterNext);
            };
            if (this.prevBtn.hasEventListener(Event.ENTER_FRAME)){
                this.prevBtn.removeEventListener(Event.ENTER_FRAME, this.onEnterPrev);
            };
            if (this.prevBtn.hasEventListener(MouseEvent.MOUSE_OUT)){
                this.prevBtn.removeEventListener(MouseEvent.MOUSE_OUT, this.onMouseUp);
            };
            if (this.nextBtn.hasEventListener(MouseEvent.MOUSE_OUT)){
                this.nextBtn.removeEventListener(MouseEvent.MOUSE_OUT, this.onMouseUp);
            };
            var _local2:int = (getTimer() - this.time);
            if (_local2 < 1000){
                if (_arg1.currentTarget == this.nextBtn){
                    this.nextNum();
                } else {
                    this.prevNum();
                };
            };
        }
        private function nextNum():void{
            if (this.currentNum < this.numMax){
                this.currentNum = (this.currentNum + this.stepNum);
                this.changeNumTxt();
            } else {
                if (this.nextBtn.hasEventListener(Event.ENTER_FRAME)){
                    this.nextBtn.removeEventListener(Event.ENTER_FRAME, this.onEnterNext);
                };
            };
        }
        private function prevNum():void{
            if (this.currentNum >= 1){
                this.currentNum = (this.currentNum - this.stepNum);
                this.changeNumTxt();
            } else {
                if (this.prevBtn.hasEventListener(Event.ENTER_FRAME)){
                    this.prevBtn.removeEventListener(Event.ENTER_FRAME, this.onEnterPrev);
                };
            };
        }
        private function maxBtnOnClick(_arg1:MouseEvent):void{
            this.currentNum = this.numMax;
            this.changeNumTxt();
        }
        private function minBtnOnClick(_arg1:MouseEvent):void{
            this.currentNum = 0;
            this.changeNumTxt();
        }
        private function changeNumTxt():void{
            if (this.currentNum >= this.numMax){
                this.dienableRight();
            } else {
                this.enableRight();
            };
            if (this.currentNum <= 0){
                this.dienableLeft();
            } else {
                this.enableLeft();
            };
            if (this.currentNum > this.numMax){
                this.currentNum = this.numMax;
            };
            if (this.currentNum < 0){
                this.currentNum = 0;
            };
            if (this.txtLayout == NumberSlider.TXT_LAYOUT_CUT){
                this.numTxt.text = ((("" + this.currentNum) + "/") + this.numMax);
            } else {
                this.numTxt.text = ("" + this.currentNum);
            };
            var _local1:UIEvent = new UIEvent(UIEvent.CHANGE, false, false, (this.currentNum - this.oldCurrentNum));
            dispatchEvent(_local1);
            this.oldCurrentNum = this.currentNum;
        }
        private function dienableRight():void{
            Sprite(this.nextBtn).mouseEnabled = false;
            Sprite(this.nextBtn).mouseChildren = false;
            this.nextBtn.filters = FilterLib.getBlackWhiteFilter();
            if (this.maxBtn){
                Sprite(this.maxBtn).mouseEnabled = false;
                Sprite(this.maxBtn).mouseChildren = false;
                this.maxBtn.filters = FilterLib.getBlackWhiteFilter();
            };
        }
        private function enableRight():void{
            Sprite(this.nextBtn).mouseEnabled = true;
            Sprite(this.nextBtn).mouseChildren = true;
            this.nextBtn.filters = null;
            if (this.maxBtn){
                Sprite(this.maxBtn).mouseEnabled = true;
                Sprite(this.maxBtn).mouseChildren = true;
                this.maxBtn.filters = null;
            };
        }
        private function dienableLeft():void{
            Sprite(this.prevBtn).mouseEnabled = false;
            Sprite(this.prevBtn).mouseChildren = false;
            this.prevBtn.filters = FilterLib.getBlackWhiteFilter();
            if (this.minBtn){
                Sprite(this.minBtn).mouseEnabled = false;
                Sprite(this.minBtn).mouseChildren = false;
                this.minBtn.filters = FilterLib.getBlackWhiteFilter();
            };
        }
        private function enableLeft():void{
            Sprite(this.prevBtn).mouseEnabled = true;
            Sprite(this.prevBtn).mouseChildren = true;
            this.prevBtn.filters = null;
            if (this.minBtn){
                Sprite(this.minBtn).mouseEnabled = true;
                Sprite(this.minBtn).mouseChildren = true;
                this.minBtn.filters = null;
            };
        }
        override public function get height():Number{
            return (this._h);
        }
        override public function set height(_arg1:Number):void{
            this._h = _arg1;
            super.updateUI();
        }
        override public function get width():Number{
            return (this._w);
        }
        override public function set width(_arg1:Number):void{
            this._w = _arg1;
            this.updateUI();
        }
        override public function updateUI():void{
            super.updateUI();
            if (this.layout == NumberSlider.LAYOUT_HORIZONTAL){
                if (((((((this.maxBtn) && (this.minBtn))) && (this.nextBtn))) && (this.prevBtn))){
                    this.numTxt.width = (((this.width - (((this.maxBtn.width + this.minBtn.width) + this.nextBtn.width) + this.prevBtn.width)) - (2 * this.btnDistance)) - (2 * this.distance));
                } else {
                    if (((this.nextBtn) && (this.prevBtn))){
                        this.numTxt.width = (((this.width - (this.nextBtn.width + this.prevBtn.width)) - (2 * this.distance)) - (2 * this.btnDistance));
                    };
                };
                this.numTxt.x = ((this.width - this.numTxt.width) / 2);
                this.numTxt.y = ((this.height - this.numTxt.height) / 2);
                if (this.txtBackground){
                    this.txtBackground.width = this.numTxt.width;
                    this.txtBackground.height = this.numTxt.height;
                    this.txtBackground.x = this.numTxt.x;
                    this.txtBackground.y = this.numTxt.y;
                };
                if (this.nextBtn){
                    this.nextBtn.x = ((this.numTxt.x + this.numTxt.width) + this.distance);
                    this.nextBtn.y = ((this.height - this.nextBtn.height) / 2);
                };
                if (this.maxBtn){
                    if (this.nextBtn){
                        this.maxBtn.x = ((this.nextBtn.x + this.nextBtn.width) + this.btnDistance);
                        this.maxBtn.y = this.nextBtn.y;
                    } else {
                        this.maxBtn.x = ((this.numTxt.x + this.numTxt.width) - this.distance);
                        this.maxBtn.y = (this.numTxt.y - 1);
                    };
                };
                if (this.prevBtn){
                    this.prevBtn.x = ((this.numTxt.x - this.prevBtn.width) - this.distance);
                    this.prevBtn.y = ((this.height - this.nextBtn.height) / 2);
                };
                if (this.minBtn){
                    if (this.prevBtn){
                        this.minBtn.x = ((this.prevBtn.x - this.prevBtn.width) - this.btnDistance);
                        this.minBtn.y = this.prevBtn.y;
                    } else {
                        this.minBtn.x = (this.numTxt.x + this.distance);
                        this.minBtn.y = (this.numTxt.y - 1);
                    };
                };
            } else {
                if (this.layout == NumberSlider.LAYOUT_VERTICAL){
                    this.txtBackground.x = ((this.width / 2) - (this.txtBackground.width / 2));
                    this.txtBackground.y = ((this.height / 2) - (this.txtBackground.height / 2));
                    this.numTxt.x = this.txtBackground.x;
                    this.numTxt.y = this.txtBackground.y;
                    if (this.nextBtn){
                        this.nextBtn.x = this.txtBackground.x;
                        this.nextBtn.y = ((this.txtBackground.y + this.txtBackground.height) + this.distance);
                    };
                    if (this.maxBtn){
                        if (this.nextBtn){
                            this.maxBtn.x = this.nextBtn.x;
                            this.maxBtn.y = ((this.nextBtn.y + this.nextBtn.height) + this.btnDistance);
                        } else {
                            this.maxBtn.x = this.txtBackground.x;
                            this.maxBtn.y = ((this.txtBackground.y + this.txtBackground.height) + this.distance);
                        };
                    };
                    if (this.prevBtn){
                        this.prevBtn.x = this.txtBackground.x;
                        this.prevBtn.y = ((this.txtBackground.y - this.prevBtn.height) - this.distance);
                    };
                    if (this.minBtn){
                        if (this.prevBtn){
                            this.minBtn.x = this.prevBtn.x;
                            this.minBtn.y = ((this.prevBtn.y - this.minBtn.height) - this.btnDistance);
                        } else {
                            this.minBtn.x = this.txtBackground.x;
                            this.minBtn.y = ((this.txtBackground.y - this.minBtn.height) - this.distance);
                        };
                    };
                };
            };
        }
        public function getNumberSliderValue():int{
            return (this.currentNum);
        }
        public function get currentNumber():int{
            return (this.currentNum);
        }
        public function set currentNumber(_arg1:int):void{
            this.currentNum = _arg1;
            this.changeNumTxt();
        }
        public function changeValue(_arg1:Number):void{
            this.currentNum = (this.currentNum + _arg1);
            this.oldCurrentNum = this.currentNum;
            this.changeNumTxt();
        }
        public function setEnabled(_arg1:Boolean):void{
            if (_arg1){
                this.numTxt.mouseEnabled = false;
            } else {
                this.numTxt.mouseEnabled = true;
            };
        }

    }
}//package com.fminutes.vinciUI.controls 
